//
//  OrderDetailController.m
//  WuJieLive
//
//  Created by iMac on 2017/12/25.
//  Copyright © 2017年 zhk. All rights reserved.
//
#define btnSize CGSizeMake(kScreenWidth / 5, 30)
#import "OrderDetailController.h"
#import "MyCollectionViewCell.h"
#import "OrderPayForController.h"
#import "CancelOrderView.h"
#import "OrderListModel.h"

@interface OrderDetailController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) NSDictionary * addressDict;
@property (nonatomic , strong) NSMutableArray * dataArray;
@property (nonatomic , strong) UIButton * headerBtn;
@property (nonatomic , strong) NSString * btnTitle;
@property (nonatomic , strong) NSString * btnImage;

@property (nonatomic , strong) NSString * totalMoney;
@property (nonatomic , strong) NSString * fareMoney;
@property (nonatomic , strong) NSString * couponMoney;
@property (nonatomic , strong) NSString * goodsNumber;

@property (nonatomic , strong) NSMutableArray * logArray;

@property (nonatomic , strong) UIView * bottomView;

// 待付款
@property (nonatomic , strong) UIButton * cancelOrderBtn;
@property (nonatomic , strong) UIButton * payForBtn;

// 待发货
@property (nonatomic , strong) UIButton * rebackMoneyBtn;

// 待收货
@property (nonatomic , strong) UIButton * checkBtn; // 查看物流
@property (nonatomic , strong) UIButton * confirmGoodsBtn;

// 待评价
@property (nonatomic , strong) UIButton * deleteBtn;
@property (nonatomic , strong) UIButton * commentBtn;
@property (nonatomic , strong) UIButton * lastCheckBtn; // 最后一选项的查看物流

@property (nonatomic , strong) UILabel * timeLabel;  // 显示倒计时
@property (nonatomic , strong) UIImageView * headerImage; //
@property (nonatomic , strong) CancelOrderView * cancelView;

@property (nonatomic , copy) NSString * orderNo; // 订单编号
@property (nonatomic , copy) NSString * payForName; // 支付方式

@end

@implementation OrderDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)setNavi
{
    self.title = @"订单详情";
}

-(void)setUI
{
    self.dataArray = [NSMutableArray array];
    self.logArray = [NSMutableArray array];
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 80, kScreenWidth, kScreenHeight - 80 - 64 - 50) style:(UITableViewStyleGrouped)];
    [self.view addSubview:self.tableview];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:nil] forCellReuseIdentifier:@"MyCollectionViewCell"];
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableview.backgroundColor = [UIColor whiteColor];
    
    self.headerImage = [[UIImageView alloc]init];
    [self.view addSubview:self.headerImage];
    [self.headerImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(60);
    }];
    self.headerImage.image = [UIImage imageNamed:@"box_订单状态"];
    
    self.headerBtn = [[UIButton alloc]init];
    [self.headerImage addSubview:self.headerBtn];
    [self.headerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset (15);
        make.centerY.equalTo(self.headerImage);
        make.right.offset(-15);
        make.height.offset(40);
    }];
    [self.headerBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    self.headerBtn.titleLabel.font = FontSize(15);
    self.headerBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.headerBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    
    self.bottomView = [[UIView alloc]init];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(50);
    }];
}

#pragma mark 订单日志
- (void)creatFooterViewWithType:(NSInteger )type
{
    UIView * view = [[UIView alloc]init];
    // 订单状态 1待付款 6待发货 11待收货 16待评论 21关闭 30完成
    if (type == 1) {
        view.frame = CGRectMake(0, 0, kScreenWidth, 65);
        UILabel * label0 = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, kScreenWidth - 30, 20)];
        label0.text = [NSString stringWithFormat:@"订单编号 : %@",self.orderNo];
        label0.textColor = TextLessImportColor;
        label0.font = FontSize(13);
        [view addSubview:label0];
        UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label0.frame), kScreenWidth - 30, 20)];
        label1.text = [NSString stringWithFormat:@"下单时间 : %@",self.logArray[0][@"create_time_format"]];
        label1.textColor = TextLessImportColor;
        label1.font = FontSize(13);
        [view addSubview:label1];
        
    }else if (type == 6){
        
        view.frame = CGRectMake(0, 0, kScreenWidth, 105);
        UILabel * label0 = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, kScreenWidth - 30, 20)];
        label0.text = [NSString stringWithFormat:@"订单编号 : %@",self.orderNo];
        label0.textColor = TextLessImportColor;
        label0.font = FontSize(13);
        [view addSubview:label0];
        UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label0.frame), kScreenWidth - 30, 20)];
        label1.text = [NSString stringWithFormat:@"下单时间 : %@",self.logArray[0][@"create_time_format"]];
        label1.textColor = TextLessImportColor;
        label1.font = FontSize(13);
        [view addSubview:label1];
        UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label1.frame), kScreenWidth - 30, 20)];
        label2.text = [NSString stringWithFormat:@"付款时间 : %@",self.logArray[1][@"create_time_format"]];
        label2.textColor = TextLessImportColor;
        label2.font = FontSize(13);
        [view addSubview:label2];
        UILabel * label3 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label2.frame), kScreenWidth - 30, 20)];
        label3.text = [NSString stringWithFormat:@"支付方式 : %@",self.payForName];
        label3.textColor = TextLessImportColor;
        label3.font = FontSize(13);
        [view addSubview:label3];
        
    }else if (type == 11){
        
        view.frame = CGRectMake(0, 0, kScreenWidth, 130);
        UILabel * label0 = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, kScreenWidth - 30, 20)];
        label0.text = [NSString stringWithFormat:@"订单编号 : %@",self.orderNo];
        label0.textColor = TextLessImportColor;
        label0.font = FontSize(13);
        [view addSubview:label0];
        UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label0.frame), kScreenWidth - 30, 20)];
        label1.text = [NSString stringWithFormat:@"下单时间 : %@",self.logArray[0][@"create_time_format"]];
        label1.textColor = TextLessImportColor;
        label1.font = FontSize(13);
        [view addSubview:label1];
        UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label1.frame), kScreenWidth - 30, 20)];
        label2.text = [NSString stringWithFormat:@"付款时间 : %@",self.logArray[1][@"create_time_format"]];
        label2.textColor = TextLessImportColor;
        label2.font = FontSize(13);
        [view addSubview:label2];
        UILabel * label3 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label2.frame), kScreenWidth - 30, 20)];
        label3.text = [NSString stringWithFormat:@"支付方式 : %@",self.payForName];
        label3.textColor = TextLessImportColor;
        label3.font = FontSize(13);
        [view addSubview:label3];
        UILabel * label4 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label3.frame), kScreenWidth - 30, 20)];
        label4.text = [NSString stringWithFormat:@"发货时间 : %@",self.logArray[2][@"create_time_format"]];
        label4.textColor = TextLessImportColor;
        label4.font = FontSize(13);
        [view addSubview:label4];
        
    }else if (type == 16){
        
        view.frame = CGRectMake(0, 0, kScreenWidth, 130);
        UILabel * label0 = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, kScreenWidth - 30, 20)];
        label0.text = [NSString stringWithFormat:@"订单编号 : %@",self.orderNo];
        label0.textColor = TextLessImportColor;
        label0.font = FontSize(13);
        [view addSubview:label0];
        UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label0.frame), kScreenWidth - 30, 20)];
        label1.text = [NSString stringWithFormat:@"下单时间 : %@",self.logArray[0][@"create_time_format"]];
        label1.textColor = TextLessImportColor;
        label1.font = FontSize(13);
        [view addSubview:label1];
        UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label1.frame), kScreenWidth - 30, 20)];
        label2.text = [NSString stringWithFormat:@"付款时间 : %@",self.logArray[1][@"create_time_format"]];
        label2.textColor = TextLessImportColor;
        label2.font = FontSize(13);
        [view addSubview:label2];
        UILabel * label3 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label2.frame), kScreenWidth - 30, 20)];
        label3.text = [NSString stringWithFormat:@"支付方式 : %@",self.payForName];
        label3.textColor = TextLessImportColor;
        label3.font = FontSize(13);
        [view addSubview:label3];
        UILabel * label4 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label3.frame), kScreenWidth - 30, 20)];
        label4.text = [NSString stringWithFormat:@"发货时间 : %@",self.logArray[2][@"create_time_format"]];
        label4.textColor = TextLessImportColor;
        label4.font = FontSize(13);
        [view addSubview:label4];
        UILabel * label5 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label4.frame), kScreenWidth - 30, 20)];
        label5.text = [NSString stringWithFormat:@"成交时间 : %@",self.logArray[3][@"create_time_format"]];
        label5.textColor = TextLessImportColor;
        label5.font = FontSize(13);
        [view addSubview:label5];
        
    }else if (type == 21){
        
        view.frame = CGRectMake(0, 0, kScreenWidth, 105);
        UILabel * label0 = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, kScreenWidth - 30, 20)];
        label0.text = [NSString stringWithFormat:@"订单编号 : %@",self.orderNo];
        label0.textColor = TextLessImportColor;
        label0.font = FontSize(13);
        [view addSubview:label0];
        UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label0.frame), kScreenWidth - 30, 20)];
        label1.text = [NSString stringWithFormat:@"下单时间 : %@",self.logArray[0][@"create_time_format"]];
        label1.textColor = TextLessImportColor;
        label1.font = FontSize(13);
        [view addSubview:label1];
        UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(label1.frame), kScreenWidth - 30, 20)];
        label2.text = [NSString stringWithFormat:@"交易关闭 : %@",self.logArray[1][@"create_time_format"]];
        label2.textColor = TextLessImportColor;
        label2.font = FontSize(13);
        [view addSubview:label2];
        
    }else if (type == 30){
        
    }
    self.tableview.tableFooterView = view;
}

-(void)setData
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken,@"order_id":self.orderID};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@order/order-detail",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            self.orderNo = responseObject[@"data"][@"order_no"];
            // 订单状态 1待付款 6待发货 11待收货 16待评论 21关闭 30完成
            if ([responseObject[@"data"][@"status_order"] integerValue] == 1) {
                self.btnImage = @"icon_倒计时_白";
                self.btnTitle = @"待付款";
                [self payFor];

                NSInteger time = [responseObject[@"data"][@"limit_times"][@"pre_pay"][@"general_info"] integerValue] + [responseObject[@"data"][@"create_time"] integerValue];
                NSInteger current = [responseObject[@"data"][@"current_time"] integerValue];
                [self creatTimeLabelNeedType:1 withCurrentTime:current withTime:time];
                
            }else if ([responseObject[@"data"][@"status_order"] integerValue] == 6){
                self.btnImage = @"icon_倒计时_白";
                self.btnTitle = @"待发货";
                [self sendGoods];
                NSInteger time = [responseObject[@"data"][@"limit_times"][@"pre_pay"][@"general_info"] integerValue] + [responseObject[@"data"][@"create_time"] integerValue];
                NSInteger current = [responseObject[@"data"][@"current_time"] integerValue];
                [self creatTimeLabelNeedType:6 withCurrentTime:current withTime:time];
            }else if ([responseObject[@"data"][@"status_order"] integerValue] == 11){
                self.btnImage = @"icon_倒计时_白";
                self.btnTitle = @"待收货";
                [self getGoods];
                NSInteger time = [responseObject[@"data"][@"limit_times"][@"pre_pay"][@"general_info"] integerValue] + [responseObject[@"data"][@"create_time"] integerValue]; // 获取有效期
                NSInteger current = [responseObject[@"data"][@"current_time"] integerValue];
                [self creatTimeLabelNeedType:11 withCurrentTime:current withTime:time];
            }else if ([responseObject[@"data"][@"status_order"] integerValue] == 16){
                self.btnImage = @"icon_交易完成_白";
                self.btnTitle = @"待评论";
                [self commont];
            }else if ([responseObject[@"data"][@"status_order"] integerValue] == 21){
                self.btnImage = @"icon_交易完成_白";
                self.btnTitle = @"交易关闭";
                [self orderClose];
            }else if ([responseObject[@"data"][@"status_order"] integerValue] == 30){
                self.btnImage = @"icon_交易完成_白";
                self.btnTitle = @"交易完成";
//                 ？？？？？？
            }
            [self.headerBtn setTitle:self.btnTitle forState:(UIControlStateNormal)];
            [self.headerBtn setImage:[UIImage imageNamed:self.btnImage] forState:(UIControlStateNormal)];
            
            self.logArray = responseObject[@"data"][@"log"];
            if ( [responseObject[@"data"][@"pay_way"][@"pay_way_name"] isKindOfClass:[NSString class] ]) {
                self.payForName = responseObject[@"data"][@"pay_way"][@"pay_way_name"];
            }
            [self creatFooterViewWithType:[responseObject[@"data"][@"status_order"] integerValue]];

            self.totalMoney = responseObject[@"data"][@"price_goods"];
            self.fareMoney = responseObject[@"data"][@"price_fare"];
            self.couponMoney = responseObject[@"data"][@"price_coupon"];
            self.goodsNumber = responseObject[@"data"][@"goods_total_number"];
            
            self.addressDict = responseObject[@"data"][@"address"];
            for (int i = 0; i < [responseObject[@"data"][@"goods"] count]; i ++) {
                OrderListModel * model = [[OrderListModel alloc]init];
                [model setValuesForKeysWithDictionary:responseObject[@"data"][@"goods"][i]];
                [self.dataArray addObject:model];
            }
            [self.tableview reloadData];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

#pragma mark 倒计时
-(void)creatTimeLabelNeedType:(NSInteger)type withCurrentTime:(NSInteger )currentTime withTime:(NSInteger)time
{
    
    //首先创建格式化对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //然后创建日期对象
    NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:time];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:currentTime];
    //创建日历
    NSCalendar * calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    //利用日历对象比较两个时间的差值
    NSDateComponents * components = [calendar components:unit fromDate:date toDate:date1 options:0];
    NSString * dateContent = nil;
    if (components.day > 0) {
        dateContent = [NSString stringWithFormat:@"剩余时间:%ld日%ld小时%ld分钟%ld秒", components.day, components.hour, components.minute, components.second];
    }else
    {
        dateContent = [NSString stringWithFormat:@"剩余时间:%ld小时%ld分钟%ld秒", components.hour, components.minute, components.second];
    }
    self.timeLabel = [[UILabel alloc]init];
    [self.headerImage addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.centerY.equalTo(self.headerBtn);
    }];
    self.timeLabel.textColor = [UIColor whiteColor];
    self.timeLabel.text =  dateContent;
    self.timeLabel.font = FontSize(13);
    
//    switch (type) {
//        case 1:
//        {
//
//        }
//            break;
//        case 6:
//        {
//
//        }
//            break;
//        case 11:
//        {
//
//        }
//            break;
//        default:
//            break;
//    }
    
}

#pragma mark 交易关闭
- (void)orderClose
{
    _deleteBtn = [[UIButton alloc]init];
    [self.bottomView addSubview:_deleteBtn];
    _deleteBtn.layer.borderColor = TextAssistColor.CGColor;
    _deleteBtn.layer.borderWidth = 0.5;
    _deleteBtn.layer.masksToBounds = YES;
    _deleteBtn.layer.cornerRadius = 10;
    [_deleteBtn setTitleColor:TextAssistColor forState:(UIControlStateNormal)];
    _deleteBtn.titleLabel.font = FontSize(13);
    [_deleteBtn setTitle:@"删除订单" forState:(UIControlStateNormal)];
    [_deleteBtn addTarget:self action:@selector(deleAction) forControlEvents:(UIControlEventTouchUpInside)];
}
#pragma mark 评论
- (void)commont
{
    _commentBtn = [[UIButton alloc]init]; // 红
    [self.bottomView addSubview:_commentBtn];
    _commentBtn.layer.borderColor = MainColor.CGColor;
    _commentBtn.layer.borderWidth = 0.5;
    _commentBtn.layer.masksToBounds = YES;
    _commentBtn.layer.cornerRadius = 10;
    [_commentBtn setTitleColor:MainColor forState:(UIControlStateNormal)];
    _commentBtn.titleLabel.font = FontSize(13);
    [_commentBtn setTitle:@"评价晒单" forState:(UIControlStateNormal)];
    [_commentBtn addTarget:self action:@selector(commentAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    _lastCheckBtn = [[UIButton alloc]init];
    [self.bottomView addSubview:_lastCheckBtn];
    _lastCheckBtn.layer.borderColor = TextAssistColor.CGColor;
    _lastCheckBtn.layer.borderWidth = 0.5;
    _lastCheckBtn.layer.masksToBounds = YES;
    _lastCheckBtn.layer.cornerRadius = 10;
    [_lastCheckBtn setTitleColor:TextAssistColor forState:(UIControlStateNormal)];
    _lastCheckBtn.titleLabel.font = FontSize(13);
    [_lastCheckBtn setTitle:@"查看物流" forState:(UIControlStateNormal)];
    [_lastCheckBtn addTarget:self action:@selector(checkAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    _deleteBtn = [[UIButton alloc]init];
    [self.bottomView addSubview:_deleteBtn];
    _deleteBtn.layer.borderColor = TextAssistColor.CGColor;
    _deleteBtn.layer.borderWidth = 0.5;
    _deleteBtn.layer.masksToBounds = YES;
    _deleteBtn.layer.cornerRadius = 10;
    [_deleteBtn setTitleColor:TextAssistColor forState:(UIControlStateNormal)];
    _deleteBtn.titleLabel.font = FontSize(13);
    [_deleteBtn setTitle:@"删除订单" forState:(UIControlStateNormal)];
    [_deleteBtn addTarget:self action:@selector(deleAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-10);
        make.right.offset(-15);
        make.size.mas_offset(btnSize);
    }];
    
    [self.lastCheckBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.commentBtn.mas_bottom);
        make.right.equalTo(self.commentBtn.mas_left).offset(-15);
        make.size.mas_offset(btnSize);
    }];
    
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.commentBtn.mas_bottom);
        make.right.equalTo(self.lastCheckBtn.mas_left).offset(-15);
        make.size.mas_offset(btnSize);
    }];
}

#pragma mark 待收货
- (void)getGoods
{
    _confirmGoodsBtn = [[UIButton alloc]init]; // 红
    [self.bottomView addSubview:_confirmGoodsBtn];
    _confirmGoodsBtn.layer.borderColor = MainColor.CGColor;
    _confirmGoodsBtn.layer.borderWidth = 0.5;
    _confirmGoodsBtn.layer.masksToBounds = YES;
    _confirmGoodsBtn.layer.cornerRadius = 10;
    [_confirmGoodsBtn setTitleColor:MainColor forState:(UIControlStateNormal)];
    _confirmGoodsBtn.titleLabel.font = FontSize(13);
    [_confirmGoodsBtn setTitle:@"确认收货" forState:(UIControlStateNormal)];
    [_confirmGoodsBtn addTarget:self action:@selector(confirmGoodsAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    _checkBtn = [[UIButton alloc]init];
    [self.bottomView addSubview:_checkBtn];
    _checkBtn.layer.borderColor = TextAssistColor.CGColor;
    _checkBtn.layer.borderWidth = 0.5;
    _checkBtn.layer.masksToBounds = YES;
    _checkBtn.layer.cornerRadius = 10;
    [_checkBtn setTitleColor:TextAssistColor forState:(UIControlStateNormal)];
    _checkBtn.titleLabel.font = FontSize(13);
    [_checkBtn setTitle:@"查看物流" forState:(UIControlStateNormal)];
    [_checkBtn addTarget:self action:@selector(checkAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.confirmGoodsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-10);
        make.right.offset(-15);
        make.size.mas_offset(btnSize);
    }];
    
    [self.checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.confirmGoodsBtn.mas_bottom);
        make.right.equalTo(self.confirmGoodsBtn.mas_left).offset(-15);
        make.size.mas_offset(btnSize);
    }];
}
#pragma mark 待发货
- (void)sendGoods
{
    _rebackMoneyBtn = [[UIButton alloc]init];
    [self.bottomView addSubview:_rebackMoneyBtn];
    _rebackMoneyBtn.layer.borderColor = TextAssistColor.CGColor;
    _rebackMoneyBtn.layer.borderWidth = 0.5;
    _rebackMoneyBtn.layer.masksToBounds = YES;
    _rebackMoneyBtn.layer.cornerRadius = 10;
    [_rebackMoneyBtn setTitleColor:TextAssistColor forState:(UIControlStateNormal)];
    _rebackMoneyBtn.titleLabel.font = FontSize(13);
    [_rebackMoneyBtn setTitle:@"申请退款" forState:(UIControlStateNormal)];
    [_rebackMoneyBtn addTarget:self action:@selector(rebackAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.rebackMoneyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-10);
        make.right.offset(-15);
        make.size.mas_offset(btnSize);
    }];
}
#pragma mark 代付款
- (void)payFor
{
    _payForBtn = [[UIButton alloc]init]; // 红
    [self.bottomView addSubview:_payForBtn];
    _payForBtn.layer.borderColor = MainColor.CGColor;
    _payForBtn.layer.borderWidth = 0.5;
    _payForBtn.layer.masksToBounds = YES;
    _payForBtn.layer.cornerRadius = 10;
    [_payForBtn setTitleColor:MainColor forState:(UIControlStateNormal)];
    _payForBtn.titleLabel.font = FontSize(13);
    [_payForBtn setTitle:@"立即付款" forState:(UIControlStateNormal)];
    [_payForBtn addTarget:self action:@selector(payForAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    _cancelOrderBtn = [[UIButton alloc]init];
    [self.bottomView addSubview:_cancelOrderBtn];
    _cancelOrderBtn.layer.borderColor = TextAssistColor.CGColor;
    _cancelOrderBtn.layer.borderWidth = 0.5;
    _cancelOrderBtn.layer.masksToBounds = YES;
    _cancelOrderBtn.layer.cornerRadius = 10;
    [_cancelOrderBtn setTitleColor:TextAssistColor forState:(UIControlStateNormal)];
    _cancelOrderBtn.titleLabel.font = FontSize(13);
    [_cancelOrderBtn setTitle:@"取消订单" forState:(UIControlStateNormal)];
    [_cancelOrderBtn addTarget:self action:@selector(cancelAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.payForBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-10);
        make.right.offset(-15);
        make.size.mas_offset(btnSize);
    }];
    
    [self.cancelOrderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.payForBtn.mas_bottom);
        make.right.equalTo(self.payForBtn.mas_left).offset(-15);
        make.size.mas_offset(btnSize);
    }];
}

#pragma mark 删除
- (void)deleAction
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken,@"order_id":self.orderID};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@order/delete-order",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            [MBProgressHUD showSuccess:@"操作成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
#pragma mark 评论
- (void)commentAction
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken,@"order_id":self.orderID};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@order/order-goods-comment",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            //            push
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
#pragma mark 付款
- (void)payForAction
{
    OrderPayForController * vc = [[OrderPayForController alloc]init];
    vc.totalMonty = self.totalMoney;
    vc.orderIDs = self.orderID;
    vc.order_type = @"1";
    BaseNavigationController * navigationVC = [[BaseNavigationController alloc]initWithRootViewController:vc];
    [self.navigationController presentViewController:navigationVC animated:YES completion:nil];
}
#pragma mark 取消订单
- (void)cancelAction
{
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@order/cancel-order",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            NSArray * reasonsArray = responseObject[@"data"][@"reasons"];
            NSMutableString * headerString = [NSMutableString string];
            for (int i = 0; i < [responseObject[@"data"][@"tips"] count]; i ++) {
                headerString = [headerString stringByAppendingString:[NSString stringWithFormat:@"\n%@",responseObject[@"data"][@"tips"][i]]].mutableCopy;
            }
            
            self.cancelView.backgroundColor = [UIColor clearColor];
            [[UIApplication sharedApplication].keyWindow addSubview:self.cancelView];
            [self.cancelView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.offset(0);
            }];
            self.cancelView.titleString = @"取消订单";
            self.cancelView.detailTextLabel.text = headerString;
            self.cancelView.dataArray = reasonsArray;
            self.cancelView.doneBlock = ^(NSInteger index) {
                [weakSelf.cancelView removeFromSuperview];
                [weakSelf cancelWithReason:responseObject[@"data"][@"reasons"][index]];
            };
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (void)cancelWithReason:(NSString *)reason
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken,@"order_id":self.orderID,@"cancel_reason":reason};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@order/cancel-order",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            [MBProgressHUD showSuccess:@"操作成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
#pragma mark 申请退款
- (void)rebackAction
{
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@order/request-refund",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            NSArray * reasonsArray = responseObject[@"data"][@"reasons"];
            NSMutableString * headerString = [NSMutableString string];
            for (int i = 0; i < [responseObject[@"data"][@"tips"] count]; i ++) {
                headerString = [headerString stringByAppendingString:[NSString stringWithFormat:@"\n%@",responseObject[@"data"][@"tips"][i]]].mutableCopy;
            }
            
            self.cancelView.backgroundColor = [UIColor clearColor];
            [[UIApplication sharedApplication].keyWindow addSubview:self.cancelView];
            [self.cancelView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.offset(0);
            }];
            self.cancelView.titleString = @"申请退款";
            self.cancelView.detailTextLabel.text = headerString;
            self.cancelView.dataArray = reasonsArray;
            self.cancelView.doneBlock = ^(NSInteger index) {
                [weakSelf.cancelView removeFromSuperview];
                [weakSelf rebackWithReason:responseObject[@"data"][@"reasons"][index]];
            };
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (void)rebackWithReason:(NSString *)reason
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken,@"order_id":self.orderID,@"cancel_reason":reason};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@order/request-refund",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            [MBProgressHUD showSuccess:@"操作成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}


#pragma mark 确认收货
- (void)confirmGoodsAction
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken,@"order_id":self.orderID};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@order/confer-receipt",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            [MBProgressHUD showSuccess:@"操作成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
#pragma mark 查看物流
- (void)checkAction
{
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        MyCollectionViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MyCollectionViewCell" forIndexPath:indexPath];
        cell.contentView.backgroundColor = Color(245, 245, 245);
        [cell bindDataWithModel:self.dataArray[indexPath.row]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else
    {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        
        return cell;
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.dataArray.count;
    }else
    {
//        return self.logArray.count;
        return 0;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 80)];
    UILabel * nameLabel = [[UILabel alloc]init];
    [view addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.top.offset(20);
    }];
    nameLabel.textColor = TextAssistColor;
    nameLabel.font = FontSize(15);
    
    UILabel * addressLabel = [[UILabel alloc]init];
    [view addSubview:addressLabel];
    [addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.top.equalTo(nameLabel.mas_bottom).offset(10);
    }];
    addressLabel.textColor = TextLessImportColor;
    addressLabel.font = FontSize(12);
    
    if (self.addressDict != nil) {
        nameLabel.text = [NSString stringWithFormat:@"%@(%@)",self.addressDict[@"consignee"],self.addressDict[@"telephone"]];
        addressLabel.text = [NSString stringWithFormat:@"%@ %@",self.addressDict[@"region_name"],self.addressDict[@"detail_address"]];
    }
    
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 115)];
    UILabel * totalLabel = [[UILabel alloc]init];
    [view addSubview:totalLabel];
    totalLabel.textColor = TextAssistColor;
    totalLabel.font = FontSize(13);
    UILabel * totalMontyLabel = [[UILabel alloc]init];
    [view addSubview:totalMontyLabel];
    totalMontyLabel.textColor = TextAssistColor;
    totalMontyLabel.font = FontSize(13);
    [totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.top.offset(15);
    }];
    [totalMontyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.centerY.equalTo(totalLabel);
    }];
    totalLabel.text = @"商品合计：";
    totalMontyLabel.text = [NSString stringWithFormat:@"¥%@",self.totalMoney];
    
    UILabel * transportationLabel = [[UILabel alloc]init];
    [view addSubview:transportationLabel];
    UILabel * transportationMontyLabel = [[UILabel alloc]init];
    [view addSubview:transportationMontyLabel];
    transportationLabel.textColor = TextAssistColor;
    transportationLabel.font = FontSize(13);
    transportationMontyLabel.textColor = TextAssistColor;
    transportationMontyLabel.font = FontSize(13);
    [transportationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.top.equalTo(totalLabel.mas_bottom).offset(5);
    }];
    [transportationMontyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.centerY.equalTo(transportationLabel);
    }];
    transportationLabel.text = @"运费：";
    transportationMontyLabel.text = [NSString stringWithFormat:@"¥%@",self.fareMoney];
    
    if (self.couponMoney.length > 0) {
        UILabel * couponLabel = [[UILabel alloc]init];
        [view addSubview:couponLabel];
        UILabel * couponMoneyLabel = [[UILabel alloc]init];
        [view addSubview:couponMoneyLabel];
        couponLabel.textColor = TextAssistColor;
        couponLabel.font = FontSize(13);
        couponMoneyLabel.textColor = TextAssistColor;
        couponMoneyLabel.font = FontSize(13);
        [couponLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(15);
            make.top.equalTo(transportationLabel.mas_bottom).offset(5);
        }];
        [couponMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-15);
            make.centerY.equalTo(couponLabel);
        }];
        couponLabel.text = @"优惠券：";
        couponMoneyLabel.text = [NSString stringWithFormat:@"¥%@",self.couponMoney];
    }
    
    UILabel * finalLabel = [[UILabel alloc]init];
    [view addSubview:finalLabel];
    [finalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.bottom.offset(-15);
    }];
    NSString * moneyString = [NSString stringWithFormat:@"¥%@",self.totalMoney];
    NSString * string = [NSString stringWithFormat:@"共%@件商品   合计:%@",self.goodsNumber,moneyString];
    NSMutableAttributedString * attString = [[NSMutableAttributedString alloc]initWithString:string attributes:@{NSForegroundColorAttributeName:TextAssistColor,NSFontAttributeName:FontSize(13)}];
    [attString setAttributes:@{NSForegroundColorAttributeName:MainColor,NSFontAttributeName:FontSize(16)} range:[string rangeOfString:moneyString]];
    finalLabel.attributedText = [[NSMutableAttributedString alloc]initWithString:string];
    finalLabel.attributedText = attString;
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 115;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 80;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 130;
    }
    return 0;
}

-(CancelOrderView *)cancelView
{
    if (!_cancelView) {
        _cancelView = [[[NSBundle mainBundle] loadNibNamed:@"CancelOrderView" owner:self options:nil] lastObject];
        
    }
    return _cancelView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
